package com.timguan.render.dataprocessor.processor;

import com.timguan.render.api.entity.cms.CMSDataProcessorParamInfo;
import com.timguan.render.dataapi.api.ICmsDataManager;
import com.timguan.render.dataapi.domain.CmsDataDO;
import com.timguan.render.dataprocessor.filter.SoldoutProductFilter;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 查询cms_data表获取对应的数据
 * Created by guankaiqiang on 2015/3/21.
 */
public class DefaultDataListProcessor extends DataProcessor {
    @Autowired
    private ICmsDataManager cmsDataManagerImpl;
    @Autowired
    private SoldoutProductFilter soldoutProductFilter;

    protected List<Object> execute(CommonParametersWrapper wrapper, Map<String, String> params) {
        if (params != null && params.get(PROCESSOR_PARAM_DATALISTID) != null) {
            final int listId = Integer.valueOf(params.get(PROCESSOR_PARAM_DATALISTID));
            final int size = StringUtils.isNotBlank(params.get(MAX_COUNT_PARAM)) && StringUtils.isNumeric(params.get(MAX_COUNT_PARAM)) ?
                    Integer.parseInt(params.get(MAX_COUNT_PARAM)) :
                    0;
            //是否是分页模块
            List<CmsDataDO> cmsDataDOs = cmsDataManagerImpl.selectByListId(listId);
            cmsDataDOs = dataFilterChain.doFilter(cmsDataDOs, new HashMap<String, String>() {{
                put(MAX_COUNT_PARAM, String.valueOf(size));
            }});
            //获取首页展示的数据
            return appendSpm(wrapper, cmsDataDOs, Constants.SPM_START);
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        needCreateDatalistWhenUsed = true;
        description = "默认";
        paramInfos = new CMSDataProcessorParamInfo[1];
        CMSDataProcessorParamInfo dataProcessorParamInfo = new CMSDataProcessorParamInfo();
        dataProcessorParamInfo.setName(MAX_COUNT_PARAM);
        dataProcessorParamInfo.setDescription("展示数量(默认全部展示)");
        paramInfos[0] = dataProcessorParamInfo;
        dataFilterChain.add(soldoutProductFilter);
    }
}
